/*
 * Logout.java
 * 
 * Version information
 * 
 * Mar 23, 2006
 * 
 * Copyright (c) AmSoft Systems, 2006
 */
package net.amsoft.iservice.icontact.webapp.struts.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.amsoft.iservice.icontact.hibernate.dataObject.LoginData;
import net.amsoft.iservice.icontact.util.IContactConstants;
import static net.amsoft.iservice.icontact.util.IContactConstants.*;
import net.amsoft.iservice.isso.client.ISSOClient;
import net.amsoft.iservice.isso.client.SAMLDataObject;
import net.amsoft.iservice.util.IServiceUtil;
import net.amsoft.iservice.util.MetaData;
import net.amsoft.iservice.util.ResolverClient;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;

/**
 * Logout is used to end the current user session. The session ends the session
 * for the contact service only and then forwards control to isso service where
 * decision is taken to end the isso session as well.
 * 
 * @author rahul
 * 
 */
public class Logout extends BaseAction {

	private static final Logger oLogger = Logger.getLogger(Logout.class);

	public ActionForward process(ActionMapping oMapping, ActionForm oForm,
			HttpServletRequest oRequest, HttpServletResponse oResponse)
			throws Exception {
		oLogger.info("process() : entry");
		LoginData oLoginData = (LoginData) oRequest.getSession(false)
				.getAttribute(SESSION_LOGIN_OBJ);
		ActionMessages oMsg = new ActionErrors();
		if (oLoginData.isLoginStatus() == false) {
			oLogger.info("process() : Already logged out");
			return oMapping.findForward(FWD_CHKLOGIN);
		}
		SAMLDataObject oSAMLData = oLoginData.getSAMLData();
		String sLoggedInSynonym = oLoginData.getLoggedInSynonym();
		String sSPXri = IContactConstants.getSPXri(oRequest.getServerName());
		MetaData oMetaData = ResolverClient.getFinalMetaData(sLoggedInSynonym);
		if (oMetaData == null) {
			oRequest.getSession(false).invalidate();
			oLogger.warn("process() : invalid metadata");
			oMsg.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
					ERR_GLOBAL_LOGOUT_NOT_DONE));
			saveErrors(oRequest, oMsg);
			return oMapping.findForward(FWD_CHKLOGIN);
		}
		String sProviderID = oMetaData.getProviderID();
		String sRelayState = IServiceUtil.generateUniqueID();
		String sLogoutRequestXML = ISSOClient.createSAMLLogoutRequest(
				sLoggedInSynonym, oSAMLData.getSessionIndex(), sSPXri,
				oMetaData.getISLOUrl());

		ActionForward oFwd = new ActionForward(ISSOClient
				.getRedirectRequestURI(oMetaData.getISLOUrl(),
						sLogoutRequestXML, sRelayState), true);
		oRequest.getSession().invalidate();
		oLoginData = new LoginData();
		oLoginData.setRelayState(sRelayState);
		oLoginData.setProviderIDHash(IServiceUtil
				.getOneWayEncValue(sProviderID));
		oLoginData.setIDPMetaDataEP(oMetaData.getServiceEndPoint());
		oRequest.getSession().setAttribute(SESSION_LOGIN_OBJ, oLoginData);
		oRequest.getSession().removeAttribute(SESSION_IBROKER_OBJ);
		oLogger.info("process() : exit, Sending logout request at: "
				+ oFwd.getPath());
		return oFwd;
	}
}
